package dxunderground.winampController.Server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Playlist {
	private ArrayList<File> songs = new ArrayList<File>();
	private Runtime rt = Runtime.getRuntime();
	private PlaylistQueue que = new PlaylistQueue(this, rt);

	public void startQueue() {
		new Thread(que).start();
	}

	@SuppressWarnings("unused")
	private int getPrevSongCount() {
		File tempDir = new File("C:/Program Files/Winamp/Song Count.txt");
		int songCount = 0;
		if (!tempDir.exists()) {
			try {
				tempDir.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			try {
				FileReader in = new FileReader(tempDir);
				BufferedReader reader = new BufferedReader(in);
				songCount = Integer.parseInt(reader.readLine());
				reader.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			} catch (NumberFormatException nfe) {
				nfe.printStackTrace();
			}
		}
		return songCount;
	}

	@SuppressWarnings("unused")
	private void setPrevSongCount(int songCount) {
		File tempDir = new File("C:\\Program Files\\Winamp\\Song Count.txt");
		try {
			FileWriter out = new FileWriter(tempDir);
			BufferedWriter writer = new BufferedWriter(out);
			writer.write(songCount + "");
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void loadPlaylist(File folder) {
		File[] list = folder.listFiles(new MusicFilter());
		if (list == null) {
			return;
		}
		for (int i = 0; i < list.length; i++) {
			if (list[i].isFile()) {
				songs.add(list[i]);
			} else {
				this.loadPlaylist(list[i]);
			}
		}
	}

	public void addSong(int songNum) {
		que.add(this.songs.get(songNum));
	}

	public void prevSong() {
		try {
			rt.exec("C:/Program Files/Winamp/Plugins/CLAmp.exe /PREV");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void playPauseSong() {
		try {
			rt.exec("C:/Program Files/Winamp/Plugins/CLAmp.exe /PLAYPAUSE");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void stopSong() {
		try {
			rt.exec("C:/Program Files/Winamp/Plugins/CLAmp.exe /STOP");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void nextSong() {
		try {
			rt.exec("C:/Program Files/Winamp/Plugins/CLAmp.exe /NEXT");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<File> getSongs() {
		return this.songs;
	}

	public int getSongCount() {
		return this.songs.size();
	}

	public int getSongNum(String song) {
		int songNum = -1;
		for (int i = 0; i < this.getSongCount(); i++) {
			if (this.songs.get(i).getAbsoluteFile().toString().equals(song)) {
				songNum = i;
				break;
			}
		}
		return songNum;
	}

	private boolean procDone(Process p) {
		try {
			p.exitValue();
			return true;
		} catch (IllegalThreadStateException e) {
			return false;
		}
	}

	/**
	 * @return Time remaining for song being played in seconds.
	 */
	@Deprecated
	public int getTimeRemaining() {
		int timeRemaining = 0;
		Runtime rt = Runtime.getRuntime();
		try {
			rt.exec("cmd");
			Process p = rt.exec("C:/Program Files/Winamp/Plugins/CLEveR.exe timeleft");
			InputStreamReader in = new InputStreamReader(p.getInputStream());
			BufferedReader stdInput = new BufferedReader(in);

			while (!procDone(p)) {
				String time = stdInput.readLine();
				String timeMin = time.substring(0, time.indexOf(":"));
				String timeSec = time.substring(time.indexOf(":") + 1);
				timeRemaining = Integer.parseInt(timeMin) * 60 + Integer.parseInt(timeSec);
			}

			stdInput.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return timeRemaining;
	}

	/**
	 * @return Status of Winamp.
	 */
	public String getStatus() {
		String status = "";
		try {
			Process p = rt.exec("C:/Program Files/Winamp/Plugins/CLAmp.exe /STATE");
			InputStreamReader in = new InputStreamReader(p.getInputStream());
			BufferedReader stdInput = new BufferedReader(in);

			while (!this.procDone(p)) {
			}
			status = stdInput.readLine();

			stdInput.close();
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return status;
	}

	public boolean notifyWhenReady() {
		boolean ret = true;
		try {
			String status = this.getStatus();
			while (PlaylistQueue.auto && PlaylistQueue.progRun && status != null && !status.equals("STOPPED")) {
				Thread.sleep(1000);
				status = this.getStatus();
			}
			// It was cutting off about a second at the end of each song
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (!PlaylistQueue.auto || !PlaylistQueue.progRun) {
			ret = false;
		}
		return ret;
	}
}
